package com.ssyu.examination.exam.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ssyu.examination.conmmon.App;
import com.ssyu.examination.conmmon.AppServlet;
import com.ssyu.examination.conmmon.PageModel;
import com.ssyu.examination.conmmon.util.DBUtil;
import com.ssyu.examination.conmmon.util.FastjsonUtil;
import com.ssyu.examination.exam.entity.ExamPlanEntity;
import com.ssyu.examination.exam.service.ExamsService;

@WebServlet("/ExamServlet")
public class ExamServlet extends AppServlet {
	private static final long serialVersionUID = 1L;
	private static ExamsService examsService = new ExamsService();

	public ExamServlet() {
		super();

	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String command = request.getParameter("command");
		if ("ExamList".equals(command)) {
			examList(request, response);
		} else if ("ExamAdd".equals(command)) {
			try {
				examAdd(request, response);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		} else if ("ExamUpdate".equals(command)) {
			try {
				examUpdate(request, response);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		} else if ("ExamGet".equals(command)) {
			examGet(request, response);
		} else {
			throw new IllegalStateException("请求无效============！");
		}
	}

	private void examGet(HttpServletRequest request, HttpServletResponse response) {
		int id = Integer.parseInt(request.getParameter("id"));
		StringBuilder sb = new StringBuilder();
		sb.append("SELECT exam_plan.id,exam_plan.exam_time_start, exam_plan.exam_time_stop,exam_plan.exam_classroom, ");
		sb.append("exam_plan.exam_paper_id,exam_plan.to_user_ids,exam_plan.to_class_id FROM ");
		sb.append("exam_plan WHERE exam_plan.id = ?");
		ExamPlanEntity examGet = DBUtil.getUniqueResult(ExamPlanEntity.class, sb.toString(), id);
		if (examGet != null) {
			print(response, FastjsonUtil.toJsonWithDateFormat(examGet, "yyyy-MM-dd HH:mm:ss"));
		}
	}

	private void examUpdate(HttpServletRequest request, HttpServletResponse response) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		int id = Integer.parseInt(request.getParameter("id"));
		Date examTimeStart = sdf.parse(request.getParameter("examTimeStart"));
		Date examTimeStop = sdf.parse(request.getParameter("examTimeStop"));
		String examClassroom = request.getParameter("examClassroom");
		int examPaperId = Integer.parseInt(request.getParameter("examPaperId"));
		String toUserIds = request.getParameter("toUserIds");
		int toClassId = Integer.parseInt(request.getParameter("toClassId"));
		int operateUserId = App.getOperateId(request, response);

		List<Object> list = new ArrayList<>();
		list.add(examTimeStart);
		list.add(examTimeStop);
		list.add(examClassroom);
		list.add(examPaperId);
		list.add(toUserIds);
		list.add(toClassId);
		list.add(operateUserId);
		list.add(id);
		StringBuilder sb = new StringBuilder();
		sb.append("UPDATE exam_plan SET exam_time_start=?, exam_time_stop=?, exam_classroom=?, exam_paper_id=?,");
		sb.append("to_user_ids=?,to_class_id=?,operate_user_id=?,operate_time=now() where id=?");
		int row = DBUtil.executeUpdate(sb.toString(), list);
		if (row == 1) {
			print(response, 1);
		} else {
			print(response, null);
		}
	}

	private void examList(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		PageModel<ExamPlanEntity> examEntity = examsService.ListExam(page, rows);
		if (examEntity != null) {
			print(response, FastjsonUtil.toJsonWithDateFormat(examEntity, "yyyy-MM-dd HH:mm:ss"));
		}
	}

	private void examAdd(HttpServletRequest request, HttpServletResponse response) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date examTimeStart = sdf.parse(request.getParameter("examTimeStart"));
		Date examTimeStop = sdf.parse(request.getParameter("examTimeStop"));
		String examClassroom = request.getParameter("examClassroom");
		String toUserIds = request.getParameter("toUserIds");
		int examPaperId = Integer.parseInt(request.getParameter("examPaperId"));
		int toClassId = Integer.parseInt(request.getParameter("toClassId"));
		int operateUserId = App.getOperateId(request, response);

		List<Object> list = new ArrayList<>();
		list.add(examTimeStart);
		list.add(examTimeStop);
		list.add(examClassroom);
		list.add(examPaperId);
		list.add(toUserIds);
		list.add(toClassId);
		list.add(operateUserId);
		list.add(1);
		StringBuilder sb = new StringBuilder();
		sb.append("INSERT INTO `exam_plan` ( `id`,`exam_time_start`, `exam_time_stop`, ");
		sb.append("`exam_classroom`, `exam_paper_id`, `to_user_ids`, `to_class_id`, `operate_user_id`, ");
		sb.append("`operate_time`, `state`, `memo`) VALUES (NULL, ?, ?, ?, ?, ?, ?, ?, NOW(), ?, NULL); ");
		int row = DBUtil.executeUpdate(sb.toString(), list);
		if (row == 1) {
			print(response, 1);
		} else {
			print(response, null);
		}
	}
}
